package com.zkdcloud.util;

import java.security.MessageDigest;

/**
 *
 * md5 util
 * @author zk123
 * @version 2018/4/30
 */
public class Md5Util {
    /**
     * md5 编码
     *
     * @param str str
     * @return after md5
     */
    public static String encoding(String str){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("md5");
            messageDigest.update(str.getBytes("UTF-8"));

            StringBuffer result = new StringBuffer();
            byte[] bytes = messageDigest.digest();
            for (int i = 0; i < bytes.length; i++) {
                if((0xff & bytes[i]) < 0x10){
                    result.append("0" + Integer.toHexString(0xff & bytes[i]));
                } else {
                    result.append(Integer.toHexString(0xff & bytes[i]));
                }
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
